package com.mathdroid.gui.menu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.KeyStroke;

import com.mathdroid.gui.profile.NewProfile;
import com.mathdroid.gui.profile.OpenProfile;
import com.mathdroid.gui.profile.SaveAs;
import com.mathdroid.model.Model;
import com.mathdroid.model.Profiles;
import com.mathdroid.persistence.Serializer;

/**
 * The menu bar of the main window.
 */
public class MenuBar extends JMenuBar {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The Menu to save/load/create profiles.
	 */
	private final JMenu profilesMenu = new JMenu("Profiles");
	
	/**
	 * Reference to this menu bar.
	 */
	private final Component parent = this;

	/**
	 * Creates the main windows menu bar.
	 */
	public MenuBar() {
		this.add(profilesMenu);
		
		buildOpenButton();
		buildSaveButton();
		buildSaveAsButton();
		buildNewProfileButton();
		
		/* Separator */
		JSeparator line = new JSeparator();
		profilesMenu.add(line);
		
		buildExitButton();
		
		JMenu toolsMenu = new JMenu("Tools");
		this.add(toolsMenu);
		
		JMenu aboutMenu = new JMenu("Help");
		this.add(aboutMenu);
	}

	/**
	 * Build the "Exit" button.
	 */
	private void buildExitButton() {
		/* Exit */
		JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_E);
		exit.setIcon(new ImageIcon(this.getClass().getResource(
				"/com/mathdroid/gui/images/door_in.png")));
		exit.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_E, ActionEvent.CTRL_MASK));
		exit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				int n = JOptionPane.showConfirmDialog(
						parent.getParent(), 
					    "Exit without saving?",
					    "Exit",
					    JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					System.exit(0);
				}
			}
		});
		profilesMenu.add(exit);
	}

	/**
	 * Build the "New Profile" button.
	 */
	private void buildNewProfileButton() {
		JMenuItem newProfile = new JMenuItem("New Profile", KeyEvent.VK_N);
		newProfile.setIcon(new ImageIcon(this.getClass().getResource(
				"/com/mathdroid/gui/images/add.png")));
		newProfile.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		newProfile.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				new NewProfile(parent);
			}
		});
		profilesMenu.add(newProfile);
	}

	/**
	 * Build the "Save as..." button.
	 */
	private void buildSaveAsButton() {
		JMenuItem saveAs = new JMenuItem("Save as...", KeyEvent.VK_S);
		saveAs.setIcon(new ImageIcon(this.getClass().getResource(
				"/com/mathdroid/gui/images/table_add.png")));
		saveAs.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_S, ActionEvent.ALT_MASK));
		saveAs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				new SaveAs(parent);
			}
		});
		profilesMenu.add(saveAs);
	}

	/**
	 * Build the "Save" button.
	 */
	private void buildSaveButton() {
		JMenuItem save = new JMenuItem("Save Profile", KeyEvent.VK_S);
		save.setIcon(new ImageIcon(this.getClass().getResource(
				"/com/mathdroid/gui/images/table_add.png")));
		save.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		save.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				Serializer s = new Serializer();
				Profiles profiles = s.getProfiles();
				profiles.put(Model.getProfile().getName(), Model.getProfile());
				s.serialize(profiles);
			}
		});
		profilesMenu.add(save);
	}

	/**
	 * Build the "Open" button.
	 */
	private void buildOpenButton() {
		JMenuItem open = new JMenuItem("Open Profile", KeyEvent.VK_O);
		open.setIcon(new ImageIcon(this.getClass().getResource(
				"/com/mathdroid/gui/images/folder.png")));
		open.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		open.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				new OpenProfile(parent);
			}
		});
		profilesMenu.add(open);
	}
}
